<?php

/**
 * EmsPortal form base class.
 *
 * @method EmsPortal getObject() Returns the current form's model object
 *
 * @package    ems1
 * @subpackage form
 * @author     Jacobo Martínez
 * @version    SVN: $Id: sfDoctrineFormGeneratedTemplate.php 29553 2010-05-20 14:33:00Z Kris.Wallsmith $
 */
abstract class BaseEmsPortalForm extends BaseFormDoctrine
{
  public function setup()
  {
    $this->setWidgets(array(
      'id'                            => new sfWidgetFormInputHidden(),
      'title'                         => new sfWidgetFormInputText(),
      'uses_register'                 => new sfWidgetFormInputCheckbox(),
      'uses_serv_buttons'             => new sfWidgetFormInputCheckbox(),
      'downloadable_contents_preview' => new sfWidgetFormInputCheckbox(),
      'uses_content_comments'         => new sfWidgetFormInputCheckbox(),
      'max_comments_per_page'         => new sfWidgetFormInputText(),
      'website_url'                   => new sfWidgetFormInputText(),
      'email'                         => new sfWidgetFormInputText(),
      'image'                         => new sfWidgetFormInputText(),
      'organization_id'               => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Organization'), 'add_empty' => false)),
      'published'                     => new sfWidgetFormInputCheckbox(),
      'is_enabled'                    => new sfWidgetFormInputCheckbox(),
      'css_id'                        => new sfWidgetFormDoctrineChoice(array('model' => $this->getRelatedModelName('Css'), 'add_empty' => true)),
      'created_at'                    => new sfWidgetFormDateTime(),
      'updated_at'                    => new sfWidgetFormDateTime(),
      'slug'                          => new sfWidgetFormInputText(),
      'can_be_accessed_by_list'       => new sfWidgetFormDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser')),
    ));

    $this->setValidators(array(
      'id'                            => new sfValidatorChoice(array('choices' => array($this->getObject()->get('id')), 'empty_value' => $this->getObject()->get('id'), 'required' => false)),
      'title'                         => new sfValidatorString(array('max_length' => 100)),
      'uses_register'                 => new sfValidatorBoolean(array('required' => false)),
      'uses_serv_buttons'             => new sfValidatorBoolean(array('required' => false)),
      'downloadable_contents_preview' => new sfValidatorBoolean(array('required' => false)),
      'uses_content_comments'         => new sfValidatorBoolean(array('required' => false)),
      'max_comments_per_page'         => new sfValidatorInteger(array('required' => false)),
      'website_url'                   => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'email'                         => new sfValidatorString(array('max_length' => 255)),
      'image'                         => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'organization_id'               => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Organization'))),
      'published'                     => new sfValidatorBoolean(array('required' => false)),
      'is_enabled'                    => new sfValidatorBoolean(array('required' => false)),
      'css_id'                        => new sfValidatorDoctrineChoice(array('model' => $this->getRelatedModelName('Css'), 'required' => false)),
      'created_at'                    => new sfValidatorDateTime(),
      'updated_at'                    => new sfValidatorDateTime(),
      'slug'                          => new sfValidatorString(array('max_length' => 255, 'required' => false)),
      'can_be_accessed_by_list'       => new sfValidatorDoctrineChoice(array('multiple' => true, 'model' => 'sfGuardUser', 'required' => false)),
    ));

    $this->validatorSchema->setPostValidator(
      new sfValidatorAnd(array(
        new sfValidatorDoctrineUnique(array('model' => 'EmsPortal', 'column' => array('title'))),
        new sfValidatorDoctrineUnique(array('model' => 'EmsPortal', 'column' => array('slug'))),
      ))
    );

    $this->widgetSchema->setNameFormat('ems_portal[%s]');

    $this->errorSchema = new sfValidatorErrorSchema($this->validatorSchema);

    $this->setupInheritance();

    parent::setup();
  }

  public function getModelName()
  {
    return 'EmsPortal';
  }

  public function updateDefaultsFromObject()
  {
    parent::updateDefaultsFromObject();

    if (isset($this->widgetSchema['can_be_accessed_by_list']))
    {
      $this->setDefault('can_be_accessed_by_list', $this->object->CanBeAccessedBy->getPrimaryKeys());
    }

  }

  protected function doSave($con = null)
  {
    $this->saveCanBeAccessedByList($con);

    parent::doSave($con);
  }

  public function saveCanBeAccessedByList($con = null)
  {
    if (!$this->isValid())
    {
      throw $this->getErrorSchema();
    }

    if (!isset($this->widgetSchema['can_be_accessed_by_list']))
    {
      // somebody has unset this widget
      return;
    }

    if (null === $con)
    {
      $con = $this->getConnection();
    }

    $existing = $this->object->CanBeAccessedBy->getPrimaryKeys();
    $values = $this->getValue('can_be_accessed_by_list');
    if (!is_array($values))
    {
      $values = array();
    }

    $unlink = array_diff($existing, $values);
    if (count($unlink))
    {
      $this->object->unlink('CanBeAccessedBy', array_values($unlink));
    }

    $link = array_diff($values, $existing);
    if (count($link))
    {
      $this->object->link('CanBeAccessedBy', array_values($link));
    }
  }

}
